package com.yinhe.module.yzx.cs;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.DateUtils;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhContent;
import com.yinhe.system.domain.YhContentStatistics;
import com.yinhe.system.service.IYhContentService;
import com.yinhe.system.service.IYhContentStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.itextpdf.text.pdf.security.LtvTimestamp.timestamp;
import static com.yinhe.common.utils.DateUtils.getDatePoor;
import static com.yinhe.module.yzx.tool.tool.autoGenericCode;
import static com.yinhe.module.yzx.tool.tool.getDatePoora;
/*import static jdk.nashorn.internal.objects.NativeString.substr;*/

/**
 * 公告浏览统计Controller
 * 
 * @author yinhe
 * @date 2022-08-01
 */
@RestController
@RequestMapping("/yzx/cs/statistics")
public class YzxContentStatisticsController extends BaseController
{
    @Autowired
    private IYhContentStatisticsService yhContentStatisticsService;
    @Autowired
    private IYhContentService yhContentService;
    /**
     * 查询公告浏览统计列表
     */
 /*   @PreAuthorize("@ss.hasPermi('yzx:cs:statistics:list')")*/
    @GetMapping("/list")
    public TableDataInfo list(YhContentStatistics yhContentStatistics)
    {
        startPage();
        List<YhContentStatistics> list = yhContentStatisticsService.selectYhContentStatisticsList(yhContentStatistics);
        return getDataTable(list);
    }
    /**
     * 导出公告浏览统计列表
     */
 /*   @PreAuthorize("@ss.hasPermi('yzx:cs:statistics:export')")*/
    @Log(title = "公告浏览统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhContentStatistics yhContentStatistics)
    {
        List<YhContentStatistics> list = yhContentStatisticsService.selectYhContentStatisticsList(yhContentStatistics);
        ExcelUtil<YhContentStatistics> util = new ExcelUtil<YhContentStatistics>(YhContentStatistics.class);
        util.exportExcel(response, list, "公告浏览统计数据");
    }

    /**
     * 获取公告浏览统计详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('yzx:cs:statistics:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhContentStatisticsService.selectYhContentStatisticsById(id));
    }

    /**
     * 新增公告浏览统计
     */
    @Log(title = "公告浏览统计", businessType = BusinessType.INSERT)
    @DeleteMapping("/s/{id}")
    public AjaxResult add(@PathVariable Long id)
    {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        String s1 =loginUser.getUsername();
        YhContent yh =new YhContent();
        yh= yhContentService.selectYhContentById(id);
        YhContentStatistics yhC1=new YhContentStatistics();
        yhC1.setNoticeId(id);
        yhC1.setUserId(sysUser.getUserId());
        yhC1.setUsername(s1);
        List<YhContentStatistics> list = yhContentStatisticsService.selectYhContentStatisticsList(yhC1);
        if(list.size()==0)
        {
            yhC1.setUserViewsCounts("1");
            yhC1.setViewsTimes("00000");
            yhC1.setStartViewsTimes(DateUtils.getNowDate());
            yhC1.setNoticeTitle(yh.getNoticeTitle());
            return toAjax(yhContentStatisticsService.insertYhContentStatistics(yhC1));
        }else
        {
            yhC1.setId(list.get(0).getId());
            yhC1.setUsername(s1);
            int q = Integer.parseInt(list.get(0).getUserViewsCounts())+1;
            String m=String.valueOf(q);
            yhC1.setViewsTimes(list.get(0).getViewsTimes());
            yhC1.setUserViewsCounts(m);
            yhC1.setStartViewsTimes(DateUtils.getNowDate());
            yhC1.setNoticeTitle(yh.getNoticeTitle());
            return toAjax(yhContentStatisticsService.updateYhContentStatistics(yhC1));
        }
    }
    /**
     * 修改公告浏览统计
     */
    @Log(title = "公告浏览统计", businessType = BusinessType.UPDATE)
    @PutMapping("/e")
    public AjaxResult edit(@RequestBody YhContent yh2)
    {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        YhContentStatistics yhC1=new YhContentStatistics();
        yhC1.setNoticeId(yh2.getId());
        yhC1.setUserId(sysUser.getUserId());
        List<YhContentStatistics> list = yhContentStatisticsService.selectYhContentStatisticsList(yhC1);
        yhC1=list.get(0);
        yhC1.setEndViewsTimes(DateUtils.getNowDate());
        String x =yhC1.getViewsTimes();
        System.out.println(x);
if(yhC1.getEndViewsTimes()==null)
{
    yhC1.setViewsTimes(yhC1.getViewsTimes());
}
else
{
    String c1 ;
    String c2 ;
    String b = getDatePoora(yhC1.getEndViewsTimes(), yhC1.getStartViewsTimes());
    String q1 = "";
    String q2 = "";
    String q3 = "";
    String q4 = "";
    if(q1=="000")
    {
        if(q3=="000")
        {
            c1="000";
        }
        else
        {
            c1=q3;
        }
    }
    else
    {
        if(q3=="000")
        {
            c1=q1;
        }
        else
        {
            int za1 = Integer.parseInt(q1);
            int za3 = Integer.parseInt(q3);
            c1 = String.valueOf(za1 + za3);
        }
    }
    if(q2=="00")
    {
        if(q4=="00")
        {
            c2="00";
        }
        else
        {
            c2=q4;
        }
    }
    else
    {
        if(q4=="00")
        {
            c2=q2;
        }
        else
        {
            int za2 = Integer.parseInt(q2);
            int za4 = Integer.parseInt(q4);

            if(za2+za4>=60)
            {
                int y=(za2+za4)%60;
                c2 = String.valueOf(y);
                int o=(za2+za4)/60;
                c1= String.valueOf(Integer.parseInt(c1)+o);
            }else
            {
                c2 = String.valueOf(za2 + za4);
            }
        }
    }
    String str11 = autoGenericCode(c1,3);
    String str12 = autoGenericCode(c2,2);
    String c3 =str11.concat(str12);
    yhC1.setViewsTimes(c3);
}
        return toAjax(yhContentStatisticsService.updateYhContentStatistics(yhC1));
    }
    /**
     * 删除公告浏览统计
     */
    @PreAuthorize("@ss.hasPermi('yzx:cs:statistics:remove')")
    @Log(title = "公告浏览统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhContentStatisticsService.deleteYhContentStatisticsByIds(ids));
    }
}
